package jmine.tec.utils.poi;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author takeshi
 */
public class SpreadsheetRow {
    private static final int BASE_YEAR = 1900;

    private final int rowNumber;

    private Map<Integer, Object> elements = new TreeMap<Integer, Object>();

    /**
     * @param row int
     */
    public SpreadsheetRow(final int row) {
        super();
        this.rowNumber = row;
    }

    /**
     * @return Map
     */
    Map<Integer, Object> getList() {
        return this.elements;
    }

    /**
     * @return int the rowNumber
     */
    public int getRowNumber() {
        return this.rowNumber;
    }

    /**
     * @return columns
     */
    public int getColumns() {
        return this.elements.size();
    }

    /**
     * @param index index
     * @return Object
     */
    public Object getRaw(final int index) {
        return this.elements.get(index);
    }

    /**
     * @param index index
     * @return {@link String}
     */
    public String getString(final int index) {
        return this.elements.get(index).toString();
    }

    /**
     * @param index index
     * @return int
     */
    public int getInteger(final int index) {
        Object el = this.getRaw(index);
        if (el instanceof Number) {
            return ((Number) el).intValue();
        }
        return Integer.parseInt(el.toString());
    }

    /**
     * @param index index
     * @return long
     */
    public long getLong(final int index) {
        Object el = this.getRaw(index);
        if (el instanceof Number) {
            return ((Number) el).longValue();
        }
        return Long.parseLong(el.toString());
    }

    /**
     * @param index index
     * @return double
     */
    public double getDouble(final int index) {
        Object el = this.getRaw(index);
        if (el instanceof Number) {
            return ((Number) el).doubleValue();
        }
        return Double.parseDouble(el.toString());
    }

    /**
     * @param index the index
     * @return Date
     */
    public Date getDate(final int index) {
        int time = this.getInteger(index);
        time -= 2;
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.HOUR_OF_DAY, 0);

        cal.set(Calendar.DAY_OF_YEAR, 1);
        cal.set(Calendar.MONTH, 1);
        cal.set(Calendar.YEAR, BASE_YEAR);
        cal.add(Calendar.DAY_OF_MONTH, time);
        return cal.getTime();
    }

}
